/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * FrmConsultaProfessor.java
 *
 * Created on 23/09/2009, 19:08:46
 */
package professor.telas;

import cursos.telas.FrmCadastroCursos;
import java.awt.Image;
import java.awt.Toolkit;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import professor.classes.ConsultaProfessorTableModel;
import professor.classes.Professor;
import professor.bd.ProfessorDAO;
import utilitarios.ItelaConsulta;

/**
 *
 * @author Gabi
 */
public class FrmConsultaProfessor extends javax.swing.JFrame {
    FrmCadastroCursos cadastroCurso;
    public FrmConsultaProfessor(ItelaConsulta telaConsulta) {
        try {
            professorDAO = new ProfessorDAO();
            List<Professor> listaProfessores = professorDAO.getProfessor();
            model = new ConsultaProfessorTableModel(listaProfessores);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(null, ex.getMessage());

            model = new ConsultaProfessorTableModel(new ArrayList<Professor>());
        }
        this.cadastroCurso = (FrmCadastroCursos) telaConsulta;
        initComponents();
        this.setLocationRelativeTo(null);
        setVisible(true);
        consultaProfessorjTable.setModel(model);
        consultaProfessorjTable.getColumnModel().getColumn(0).setPreferredWidth(200);
        consultaProfessorjTable.getColumnModel().getColumn(1).setPreferredWidth(90);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        jToolBar1 = new javax.swing.JToolBar();
        adicionarProfessorjButton = new javax.swing.JButton();
        editarProfessorjButton = new javax.swing.JButton();
        excluirProfessorjButton = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JToolBar.Separator();
        imprimirjButton = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        consultaProfessorjTable = new javax.swing.JTable();
        nomeProfessorjTextField = new javax.swing.JTextField();
        nomeProfessorjLabel = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Consulta de Professores");
        setLocationByPlatform(true);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
        URL url = this.getClass().getResource("/imagens/logo_mini.png");
        Image imagemTitulo = Toolkit.getDefaultToolkit().getImage(url);
        this.setIconImage(imagemTitulo);
        try{
            UIManager.setLookAndFeel("com.birosoft.liquid.LiquidLookAndFeel");
        }catch (Exception e){
            e.printStackTrace();
        }
        jPanel1.setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jToolBar1.setFloatable(false);
        jToolBar1.setRollover(true);

        adicionarProfessorjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/professormais.png"))); // NOI18N
        adicionarProfessorjButton.setText("Adicionar");
        adicionarProfessorjButton.setToolTipText("Adicionar Professor");
        adicionarProfessorjButton.setFocusable(false);
        adicionarProfessorjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        adicionarProfessorjButton.setMaximumSize(new java.awt.Dimension(90, 80));
        adicionarProfessorjButton.setMinimumSize(new java.awt.Dimension(90, 80));
        adicionarProfessorjButton.setPreferredSize(new java.awt.Dimension(90, 80));
        adicionarProfessorjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        adicionarProfessorjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                adicionarProfessorActionPerformed(evt);
            }
        });
        jToolBar1.add(adicionarProfessorjButton);

        editarProfessorjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/professoredit.png"))); // NOI18N
        editarProfessorjButton.setText("Editar");
        editarProfessorjButton.setToolTipText("Editar Professor");
        editarProfessorjButton.setEnabled(false);
        editarProfessorjButton.setFocusable(false);
        editarProfessorjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        editarProfessorjButton.setMaximumSize(new java.awt.Dimension(90, 80));
        editarProfessorjButton.setMinimumSize(new java.awt.Dimension(90, 80));
        editarProfessorjButton.setPreferredSize(new java.awt.Dimension(90, 80));
        editarProfessorjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        editarProfessorjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editarProfessorActionPerformed(evt);
            }
        });
        jToolBar1.add(editarProfessorjButton);

        excluirProfessorjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/professormenos.png"))); // NOI18N
        excluirProfessorjButton.setText("Excluir");
        excluirProfessorjButton.setToolTipText("Excluir Professor");
        excluirProfessorjButton.setFocusable(false);
        excluirProfessorjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        excluirProfessorjButton.setMaximumSize(new java.awt.Dimension(90, 80));
        excluirProfessorjButton.setMinimumSize(new java.awt.Dimension(90, 80));
        excluirProfessorjButton.setPreferredSize(new java.awt.Dimension(90, 80));
        excluirProfessorjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        excluirProfessorjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                excluirProfessorjButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(excluirProfessorjButton);
        jToolBar1.add(jSeparator1);

        imprimirjButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/impressao.png"))); // NOI18N
        imprimirjButton.setText("Imprimir");
        imprimirjButton.setToolTipText("Imprimir");
        imprimirjButton.setFocusable(false);
        imprimirjButton.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        imprimirjButton.setMaximumSize(new java.awt.Dimension(90, 80));
        imprimirjButton.setMinimumSize(new java.awt.Dimension(90, 80));
        imprimirjButton.setPreferredSize(new java.awt.Dimension(90, 80));
        imprimirjButton.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        imprimirjButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                imprimirjButtonActionPerformed(evt);
            }
        });
        jToolBar1.add(imprimirjButton);

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagens/voltar.png"))); // NOI18N
        jButton5.setText("Voltar");
        jButton5.setToolTipText("Voltar");
        jButton5.setFocusable(false);
        jButton5.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        jButton5.setMaximumSize(new java.awt.Dimension(90, 80));
        jButton5.setMinimumSize(new java.awt.Dimension(90, 80));
        jButton5.setPreferredSize(new java.awt.Dimension(90, 80));
        jButton5.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                voltarTela(evt);
            }
        });
        jToolBar1.add(jButton5);

        jPanel1.add(jToolBar1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 260, 470, -1));

        consultaProfessorjTable.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Nome do Professor", "Celular", "Curso"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        consultaProfessorjTable.getTableHeader().setReorderingAllowed(false);
        consultaProfessorjTable.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                consultaProfessorjTableMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(consultaProfessorjTable);
        consultaProfessorjTable.getColumnModel().getColumn(0).setMinWidth(200);
        consultaProfessorjTable.getColumnModel().getColumn(0).setPreferredWidth(200);
        consultaProfessorjTable.getColumnModel().getColumn(0).setMaxWidth(200);
        consultaProfessorjTable.getColumnModel().getColumn(1).setMinWidth(90);
        consultaProfessorjTable.getColumnModel().getColumn(1).setPreferredWidth(90);
        consultaProfessorjTable.getColumnModel().getColumn(1).setMaxWidth(90);

        jPanel1.add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 70, 460, 170));

        nomeProfessorjTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                nomeProfessorjTextFieldKeyPressed(evt);
            }
        });
        jPanel1.add(nomeProfessorjTextField, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 40, 460, -1));

        nomeProfessorjLabel.setText("Nome do Professor");
        jPanel1.add(nomeProfessorjLabel, new org.netbeans.lib.awtextra.AbsoluteConstraints(20, 23, -1, -1));

        getContentPane().add(jPanel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(0, 0, 500, 370));

        pack();
    }// </editor-fold>//GEN-END:initComponents
    private void adicionarProfessorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_adicionarProfessorActionPerformed
        new FrmCadastroProfessor(new Professor());
}//GEN-LAST:event_adicionarProfessorActionPerformed
    private void voltarTela(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_voltarTela
        this.dispose();
    }//GEN-LAST:event_voltarTela
    private void editarProfessorActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editarProfessorActionPerformed
        int linha = consultaProfessorjTable.getSelectedRow();
        if (linha >= 0) {
            Professor professor = model.listaProfessor.get(linha);
            new FrmCadastroProfessor(professor);
        }
    }//GEN-LAST:event_editarProfessorActionPerformed
    private void consultaProfessorjTableMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_consultaProfessorjTableMouseClicked
        if (evt.getClickCount() == 1) {
            editarProfessorjButton.setEnabled(true);
        }
        if (evt.getClickCount() == 2 && cadastroCurso != null) {
            int linha = consultaProfessorjTable.getSelectedRow();
            if (linha >= 0) {
                Professor professor = model.listaProfessor.get(linha);
                cadastroCurso.setData(professor);
                this.dispose();
            }
        }
    }//GEN-LAST:event_consultaProfessorjTableMouseClicked
    private void excluirProfessorjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_excluirProfessorjButtonActionPerformed
        int linha = consultaProfessorjTable.getSelectedRow();
        if (linha >= 0) {
            Professor professor = model.listaProfessor.get(linha);
            try {
                professorDAO.excluir(professor);
                JOptionPane.showMessageDialog(this, "Dados excluidos com sucesso");
            } catch (Exception ex) {
                ex.printStackTrace();
                JOptionPane.showMessageDialog(this, "Dados não foram excluidos");
            }
        }
    }//GEN-LAST:event_excluirProfessorjButtonActionPerformed
    private void nomeProfessorjTextFieldKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_nomeProfessorjTextFieldKeyPressed
        String professor = nomeProfessorjTextField.getText();
        List listaProfessor = professorDAO.pesquisaProfessor(professor);
        model.listaProfessor = listaProfessor;
        model.fireTableDataChanged();
    }//GEN-LAST:event_nomeProfessorjTextFieldKeyPressed
    private void imprimirjButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_imprimirjButtonActionPerformed
    }//GEN-LAST:event_imprimirjButtonActionPerformed
    /**
     * @param args the command line arguments
     */
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton adicionarProfessorjButton;
    private javax.swing.JTable consultaProfessorjTable;
    private javax.swing.JButton editarProfessorjButton;
    private javax.swing.JButton excluirProfessorjButton;
    private javax.swing.JButton imprimirjButton;
    private javax.swing.JButton jButton5;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JToolBar.Separator jSeparator1;
    private javax.swing.JToolBar jToolBar1;
    private javax.swing.JLabel nomeProfessorjLabel;
    private javax.swing.JTextField nomeProfessorjTextField;
    // End of variables declaration//GEN-END:variables
    private ProfessorDAO professorDAO;
    private ConsultaProfessorTableModel model;
}

